/*
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package com.sys.midware.zookeeper.sample.listener;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateListener;

import com.sys.midware.zookeeper.curator.IZKRegistryCenter;
import com.sys.midware.zookeeper.listener.AbstractListener;
import com.sys.midware.zookeeper.listener.AbstractListenerManager;
import com.sys.midware.zookeeper.sample.facade.SampleZookeeperFacade;
import com.sys.midware.zookeeper.sample.node.SampleJobNode;

/**
 * 作业控制监听管理器.
 * 
 * @author zhangliang
 */
public class SampleJobListenerManager extends AbstractListenerManager {
    private SampleJobNode jobNode;

    IZKRegistryCenter zk = null;

    // TreeCache cache =null;
    public SampleJobListenerManager() {
        super();
        zk = SampleZookeeperFacade.zkClientMap.get(SampleZookeeperFacade.NAMESPACE_JOB);
        super.setZK(zk);
        super.cacheData(SampleJobNode.ROOT);
        jobNode = new SampleJobNode();
    }

    @Override
    public void start() {
        addConnectionStateListener(new ConnectionLostListener());
        addDataListener(new JobStatusListener());
    }

    /**
     * 失去连接监听 <b>@author：</b> mobing <br/>
     * <b>@date：</b> 2016年6月1日 上午11:12:06 <br/>
     * <b>@version: </b> <br/>
     */
    class ConnectionLostListener implements ConnectionStateListener {
        @Override
        public void stateChanged(final CuratorFramework client, final ConnectionState newState) {
            System.out.println("--------stateChanged------" + newState);
            if (ConnectionState.LOST == newState) {

            } else if (ConnectionState.RECONNECTED == newState) {

            }
        }
    }

    public SampleJobNode getJobNode() {
        return jobNode;
    }

    public void setJobNode(SampleJobNode jobNode) {
        this.jobNode = jobNode;
    }

    class JobStatusListener extends AbstractListener {

        @Override
        protected void dataChanged(final CuratorFramework client, final TreeCacheEvent event, final String path) {
            try {
                System.out.println("--------path--------" + path);
                if (!jobNode.isStatusNode(path)) {
                    return;
                }

                System.out.println("-getJobNamePath----" + path + "-----" + jobNode.getJobNamePath(path));
                String jobName = zk.getDataFromCached(jobNode.getJobNamePath(path));

                System.out.println("jobName:" + jobName);
                ChildData childData = event.getData();
                String data = new String(childData.getData());
                String status = data;
                if (1 == Integer.valueOf(status)) {

                } else if (2 == Integer.valueOf(status)) {

                } else if (3 == Integer.valueOf(status)) {

                }
                System.out.println("---status--" + status);
            } catch (Exception e) {
                
                // TODO Auto-generated catch block
                e.printStackTrace();
                
            }
        }
    }

}
